package io.plus.user_interface.io.server.command.command_handler;

import drds.common.$;
import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.user_interface.io.common.Status;
import io.plus.user_interface.io.common.io;
import io.plus.user_interface.io.common.message.MessageHelper;
import io.plus.user_interface.io.common.packet.*;
import io.plus.user_interface.io.common.protocol.PacketImpl;
import io.plus.user_interface.io.common.utils.ImKit;
import io.plus.user_interface.io.common.utils.JsonKit;
import io.plus.user_interface.io.server.command.AbstractCommandHandler;

import java.util.ArrayList;
import java.util.List;

//import drds.io.common.packet.*;
//import user_interface.io.common.packet.*;

/**
 * 获取用户信息消息命令
 */
public class UserReqHandler extends AbstractCommandHandler {

    /**
     * 处理在线用户好友及群组用户;
     *
     * @param groups
     * @param flag(0：好友,1:群组)
     * @return
     */
    private static List<Group> initOnlineUserFriendsGroups(List<Group> groups, Integer type, Integer flag) {
        if (groups == null || groups.isEmpty()) {
            return null;
        }
        //处理好友分组在线用户相关信息;
        List<Group> onlineGroups = new ArrayList<Group>();
        for (Group group : groups) {
            Group copyGroup = ImKit.copyGroupWithoutUsers(group);
            List<User> users = null;
            if (flag == 1) {
                users = io.getAllUserByGroup(group.getId());
            } else if (flag == 0) {
                users = group.getUsers();
            }
            if (users != null && !users.isEmpty()) {
                List<User> copyUsers = new ArrayList<User>();
                for (User userObj : users) {
                    User onlineUser = io.getUser(userObj.getId());
                    //在线
                    if (onlineUser != null && type == 0) {
                        User copyOnlineUser = ImKit.copyUserWithoutFriendsGroups(onlineUser);
                        copyUsers.add(copyOnlineUser);
                        //离线
                    } else if (onlineUser == null && type == 1) {
                        User copyOnlineUser = ImKit.copyUserWithoutFriendsGroups(onlineUser);
                        copyUsers.add(copyOnlineUser);
                    }
                }
                copyGroup.setUsers(copyUsers);
            }
            onlineGroups.add(copyGroup);
        }
        return onlineGroups;
    }

    @Override
    public Command command() {
        return Command.get_user_info_request;
    }

    /**
     * 根据用户id获取用户在线及离线用户;
     *
     * @param userid
     * @param type(0:所有在线用户,1:所有离线用户,2:所有用户[在线+离线])
     * @return
     */
    public User getUserInfo(String userid, Integer type) {
        User user = null;

        //消息持久化助手;
        MessageHelper messageHelper = userConfiguration.getMessageHelper();
        boolean isStore = true;
        if (isStore) {
            user = messageHelper.getUserByType(userid, 2);
            if (user == null) {
                return null;
            }
            user.setFriends(messageHelper.getAllFriendUsers(userid, type));
            user.setGroups(messageHelper.getAllGroupUsers(userid, type));
            return user;
        } else {
            user = io.getUser(userid);
            if (user == null) {
                return null;
            }
            User copyUser = ImKit.copyUserWithoutFriendsGroups(user);
            //在线用户;
            if (type == 0 || type == 1) {
                //处理好友分组在线用户相关信息;
                List<Group> friends = user.getFriends();
                List<Group> onlineFriends = initOnlineUserFriendsGroups(friends, type, 0);
                if (onlineFriends != null) {
                    copyUser.setFriends(onlineFriends);
                }
                //处理群组在线用户相关信息;
                List<Group> groups = user.getGroups();
                List<Group> onlineGroups = initOnlineUserFriendsGroups(groups, type, 1);
                if (onlineGroups != null) {
                    copyUser.setGroups(onlineGroups);
                }
                return copyUser;
                //所有用户(在线+离线);
            } else if (type == 2) {
                return user;
            }
        }
        return user;
    }

    @Override
    public PacketImpl handler(PacketImpl packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) throws Exception {
        UserRequestMessage userReqBody = JsonKit.toBean(packet.getBody(), UserRequestMessage.class);
        User user = null;
        ResponseMessage resPacket = null;

        String userId = userReqBody.getUserId();
        if ($.isNullOrEmpty(userId)) {
            return ImKit.convertResponsepPacket(new ResponseMessage(Command.get_user_info_response, Status.C10004), asynchronousSocketChannelContext);
        }
        //(0:所有在线用户,1:所有离线用户,2:所有用户[在线+离线]);
        Integer type = userReqBody.getType() == null ? 2 : userReqBody.getType();
        if (0 == userReqBody.getType() || 1 == userReqBody.getType() || 2 == userReqBody.getType()) {
            user = getUserInfo(userId, type);
            //在线用户
            if (0 == userReqBody.getType()) {
                resPacket = new ResponseMessage(Command.get_user_info_response, Status.C10005);
                //离线用户;
            } else if (1 == userReqBody.getType()) {
                resPacket = new ResponseMessage(Command.get_user_info_response, Status.C10006);
                //在线+离线用户;
            } else if (2 == userReqBody.getType()) {
                resPacket = new ResponseMessage(Command.get_user_info_response, Status.C10003);
            }
        }
        if (user == null) {
            return ImKit.convertResponsepPacket(new ResponseMessage(Command.get_user_info_response, Status.C10004), asynchronousSocketChannelContext);
        }
        resPacket.setData(user);
        return ImKit.convertResponsepPacket(resPacket, asynchronousSocketChannelContext);
    }
}
